/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import adc_lcard.ADCE140Driver;
import datasource.DynamixelSource;
import dynamixel.Actuator;
import dynamixel.HostActuator;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Asko
 */
public class AdcTest extends javax.swing.JFrame {

    ActScriptAnalyzer analyzer;
    int size = 1024;
    double[] bufX = new double[size];
    double[] bufY = new double[size];
    DynamixelSource dynSource [] = new DynamixelSource[4];
    int cursor = 0;
    HostActuator host;
    int baudRate = 57142;
    int actCnt = 4;
    Actuator[] acts = new Actuator[actCnt];
    boolean isScan = false;
    Thread th;

    public AdcTest() {
        /*host = new HostActuator(baudRate);
        for (int i = 0; i < 4; i++) {
            acts[i] = new Actuator(host, i+1);
            acts[i].setTorqueEnable(true);
            dynSource[i] = new DynamixelSource(acts[i], HostActuator.
                    PRESENT_LOAD_L, 2);
        }*/
        ADCE140Driver.init();
        ADCE140Driver.setShitFiltering(false);
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Start = new javax.swing.JButton();
        j_combo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        j_tf = new javax.swing.JTextField();
        Start1 = new javax.swing.JButton();
        j_plotPanel = new org.math.plot.Plot2DPanel();
        jsp_adc = new org.min.gui.JSlidePlotter();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Start.setText("Start");
        Start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartActionPerformed(evt);
            }
        });

        j_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", " " }));

        jLabel1.setText("Val");

        Start1.setText("Stop");
        Start1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Start1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jsp_adcLayout = new javax.swing.GroupLayout(jsp_adc);
        jsp_adc.setLayout(jsp_adcLayout);
        jsp_adcLayout.setHorizontalGroup(
            jsp_adcLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jsp_adcLayout.setVerticalGroup(
            jsp_adcLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(j_plotPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jsp_adc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Start, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Start1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(54, 54, 54)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(j_tf, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(j_combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(j_plotPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                    .addComponent(jsp_adc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Start)
                    .addComponent(j_combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(j_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Start1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartActionPerformed
        isScan = true;
        th = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isScan) {
                    try {
                        /*bufY[cursor] = dynSource[j_combo.getSelectedIndex()].
                                getData(0, 0);*/
                        bufY[cursor] = ADCE140Driver.getGdata(j_combo.getSelectedIndex()+1);
                        if (bufY[cursor] != -1) {
                            bufX[cursor] = cursor;
                            jsp_adc.addValue(bufY[cursor]);
                            if (cursor == size - 1) {
                                cursor = 0;
                                j_plotPanel.removeAllPlots();
                                j_plotPanel.addLinePlot("", bufX, bufY);
                            } else {
                                cursor++;
                            }
                            Thread.sleep(10);
                        }
                    } catch (InterruptedException ex) {
                        Logger.getLogger(AdcTest.class.getName()).
                                log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        th.start();
    }//GEN-LAST:event_StartActionPerformed

    private void Start1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Start1ActionPerformed
        isScan = false;
        th = null;
    }//GEN-LAST:event_Start1ActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws InterruptedException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;


                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdcTest.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdcTest.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdcTest.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdcTest.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AdcTest().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Start;
    private javax.swing.JButton Start1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox j_combo;
    private org.math.plot.Plot2DPanel j_plotPanel;
    private javax.swing.JTextField j_tf;
    private org.min.gui.JSlidePlotter jsp_adc;
    // End of variables declaration//GEN-END:variables
}
